﻿/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

package com.seyself.proxy 
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.describeType;

dynamic public class TestProxy extends Sprite
{
    
    
    
    public function TestProxy(target:DisplayObject) 
    {
        setProxyMethods(target);
    }
    
    public function setProxyMethods(target:DisplayObject):void
	{
		var data:XML = describeType(target);
		var n:uint, i:uint, name:String;
		var methods:XMLList = data.elements("method");
		n = methods.length();
		for (i = 0; i < n; i++ )
		{
			name = methods[i].@name;
			if (!this.hasOwnProperty(name))
			{
				this[name] = function():*
				{
					var func:Function = target[arguments.callee.methodName];
					return func.apply(target, arguments);
				}
				this[name].methodName = name;
			}
		}
		
		var accessor:XMLList = data.elements("accessor");
		n = accessor.length();
		for (i = 0; i < n; i++ )
		{
			name = accessor[i].@name;
			if (!this.hasOwnProperty(name))
			{
				this[name] = function():*
				{
					if (arguments.length == 1) 
						return target[arguments.callee.propName] = arguments[0];
					return target[arguments.callee.propName];
				}
				this[name].propName = name;
			}
		}
	}
    
}

}